/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.date.DateUtil;
import com.wubo.sec.filter.ActionLogBeforeFilter;
import com.wubo.sec.model.ActionLog;
import com.wubo.sec.service.IActionLogService;
import com.wubo.sec.service.IGroupService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-9
 * @version 1.0.01
 */
@Controller
public class ActionLogMgtAction extends BaseSecAction {
	@Autowired
	private IActionLogService actionLogService;
	@Autowired
	private IGroupService groupService;
	
	private String actionName;
	
	private String userName;
	
	private String startDate;
	
	private String endDate;
	
	private String ids;
	
	private String orderby;
	
	private List<ActionLog> logs;
	
	public IActionLogService getActionLogService() {
		return actionLogService;
	}

	public void setActionLogService(IActionLogService actionLogService) {
		this.actionLogService = actionLogService;
	}

	public IGroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<ActionLog> getLogs() {
		return logs;
	}

	public void setLogs(List<ActionLog> logs) {
		this.logs = logs;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getOrderby() {
		return orderby;
	}

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public String list() throws Exception {
		StringBuilder where = new StringBuilder("where 1=1");
		if(! StringUtils.isEmpty(actionName)){
			where.append( " and l.actionName like '%"+actionName+"%'" );
		}
		if(! StringUtils.isEmpty(userName)){
			where.append( " and ( l.user.username like '%"+userName+"%' or l.user.truename like '%"+userName+"%')" );
		}
		List<Date> params = new ArrayList<Date>(2);
		if(! StringUtils.isEmpty(startDate)){
			params.add(DateUtil.StringToDate(startDate));
			where.append(" and l.actionTime >= ?");
		}
		if(! StringUtils.isEmpty(endDate)){
			params.add(DateUtil.StringToDate(endDate+" 23:59:59"));
			where.append(" and l.actionTime <= ?");
		}
		
		where.append(new ActionLogBeforeFilter(groupService).sift());
		
		if(StringUtils.isEmpty(orderby)){
			orderby = " order by l.actionTime desc";
		}

		Object[] args = params.toArray();
		
		total= actionLogService.getTotal(where.toString(), args);
		
		logs = actionLogService.getSplitPage(start, limit>0?limit:20, where.toString()+orderby, args);
		
		return SUCCESS;
	}

	private String q; //用于AutoComplete
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public void autoCompleteForName() {
		try{
			List<String> names = actionLogService.listName(q);
			json(new JSONSerializer().serialize(names));
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(null);
		}
	}
	
	public void autoCompleteForUser() {
		try{
			List<String[]> usernames = actionLogService.listUser(q);
			json(new JSONSerializer().serialize(usernames));
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(null);
		}
	}
	
	public String del() throws Exception {
		if(! StringUtils.isEmpty(ids))
			actionLogService.delete(ids);
		
		list();
		
		flag = true;
		
		return SUCCESS;
	}
	
	public String clearAll() throws Exception {
		actionLogService.clearAll();
		
		list();
		
		flag = true;
		
		return SUCCESS;
	}
	
}
